package com.example.associationmanager.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.associationmanager.bean.*;
import com.example.associationmanager.service.PeopleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@Slf4j
@Controller
public class PeopleTableController {//社员信息接口
    @Autowired
    private PeopleService peopleService;

    @ResponseBody
    @GetMapping("/select/peopleAssociation/all")//获取社员及所属社团的信息
    public Object peopleAssociation(Integer page, Integer limit, HttpServletRequest request){
        User user= (User) request.getSession().getAttribute("loginUser");//从session中获取用户属于的社团
        Integer communityNumber= user.getCommunityNumber();


        Integer status=0;//仅显示已经被批准入社的社员
        List<PeopleAssociationVo> allPeopleAssociation = peopleService.getAllPeopleAssociation(page, limit, status,communityNumber);
        Integer count=peopleService.getAllPeopleAssociation(1, Integer.MAX_VALUE, status,communityNumber).size();
//        /**
//         * 测试代码，已注释
//         */
//        Map<String, Object> data=new LinkedHashMap<>();
//        data.put("code",0);
//        data.put("msg","");
//        data.put("count",100);
//        data.put("data",peopleService.list());
//        List list = peopleService.list();
        log.info("/select/peopleAssociation/all接口，当前请求的分页及每页限制数量："+page + "\t"+limit);
        Msg msg = new Msg(0,"",count, allPeopleAssociation);
        return msg;
    }
    @ResponseBody
    @GetMapping("/select/peopleAssociation/get")//获取申请中的人员数据
    public Object peopleAssociationGet(Integer page,Integer limit,HttpServletRequest request){
        User user= (User) request.getSession().getAttribute("loginUser");//从session中获取用户属于的社团
        Integer communityNumber= user.getCommunityNumber();
        Integer status=1;//申请中的人员，状态0:已批准入社,1:申请中,2:退社，3:审批未通过
        List<PeopleAssociationVo> allPeopleAssociation = peopleService.getAllPeopleAssociation(page, limit, status,communityNumber);
        Integer count=peopleService.getAllPeopleAssociation(1, Integer.MAX_VALUE, status,communityNumber).size();
        log.info("/select/peopleAssociation/all接口，当前请求的分页及每页限制数量："+page + "\t"+limit);
        Msg msg = new Msg(0,"",count, allPeopleAssociation);
        return msg;
    }
    @ResponseBody
    @GetMapping ("/people/apply/pass")//获取审批通过的社员信息
    public Object peoplePass(@RequestParam("peopleNumber") String peopleNumber) {
        Integer peopleStatus=0;
        People people=new People(peopleNumber,null,null,null,null,null,null,null,null,peopleStatus);
        peopleService.updateById(people);
        Msg msg = new Msg(200,"success",null,null);
        return msg;
    }
    @ResponseBody
    @GetMapping ("/people/apply/no")//审批不通过，标注为3(申请失败)
    public Object peoplePassNo(@RequestParam("peopleNumber") String peopleNumber) {
        Integer peopleStatus=3;//状态0:已批准入社,1:申请中,2:退社，3:审批未通过
        People people=new People(peopleNumber,null,null,null,null,null,null,null,null,peopleStatus);
        peopleService.updateById(people);
        Msg msg = new Msg(200,"success",null,null);
        return msg;
    }
    @ResponseBody
    @PostMapping("/peopleAssociation/add")//添加人员数据
    public Object peopleAssociationAdd(String peopleNumber,String peopleName,String peopleGender,Integer peopleAge,String peopleClass,String peoplePhone,String peopleElses,Integer communityNumber) {
        People people_number = peopleService.getOne(new QueryWrapper<People>().eq("people_number", peopleNumber));
        if (people_number != null){//学号已经存在的情况
            Msg msg = new Msg(200,"failure",null,null);
            return msg;
        }
        String peopleRank="普通社员";//第一次加入社团，身份强制为普通社员，后期可由管理员进行更改
        Integer peopleStatus=1;//添加社员默认状态为申请状态
        People people= new People(peopleNumber,peopleName,peopleGender,peopleAge,peopleClass,peoplePhone,peopleRank,peopleElses,communityNumber,peopleStatus);
        peopleService.save(people);//向数据库进行人员对象的插入
        Msg msg = new Msg(200,"success",null,null);
        return msg;

    }
    @ResponseBody
    @GetMapping ("/people/delete")//删除人员数据
    public Object peopleDelete(@RequestParam("peopleNumber") String peopleNumber) {

        peopleService.removeById(peopleNumber);
        Msg msg = new Msg(200,"success",null,null);
        return msg;
    }
    @ResponseBody
    @PostMapping("/people/update")//更新人员数据
    public Object peopleAssociationUpdate(String peopleNumber,String peopleName,String peopleGender,Integer peopleAge,String peopleClass,String peoplePhone,String peopleRank,String peopleElses,Integer communityNumber) {
        Integer peopleStatus=1;//默认申请中，需管理员同意后才可入团(值为0)

        People people= new People(peopleNumber,peopleName,peopleGender,peopleAge,peopleClass,peoplePhone,peopleRank,peopleElses,communityNumber,peopleStatus);
        peopleService.updateById(people);
        Msg msg = new Msg(200,"success",null,null);
        return msg;

    }
}
